package com.example.hcd.functionactivity;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.hcd.R;
import com.example.hcd.datamanage.UserInfo;
import com.example.hcd.internet.LinkHelp;
import com.example.hcd.internet.URLS;
import com.example.hcd.tool.ToastHelp;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyPwActivity extends BaseActivity implements OnClickListener{
	private UserInfo userInfo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mPageName = "ModifyPwActivity";
		setContentView(R.layout.modify_pwd_view);
		userInfo = UserInfo.getUserInfor();
		// 返回键
		ImageView backBtn = (ImageView) findViewById(R.id.title_bar_btn_left);
		// 提交键
		ImageView uploadBtn = (ImageView) findViewById(R.id.title_bar_btn_right);
		backBtn.setOnClickListener(this);
		uploadBtn.setOnClickListener(this);
		
		TextView text = (TextView)findViewById(R.id.title31);
		text.setText(UserInfo.getUserInfor().getLastModifyPdTime());
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int id = arg0.getId();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
		switch (id) {
		case R.id.title_bar_btn_left:
			finish();
			break;
		case R.id.title_bar_btn_right:
			if(isCanUpload())
			{
				UpdateLoadInfo upload = new UpdateLoadInfo();
				upload.execute();
			}
			break;
		default:
			break;
		}
	}
	
	/*
	 * 传送数据到服务器
	 */
	private class UpdateLoadInfo extends AsyncTask<String, Integer, String> {
		// private ProgressDialog mProgress;
		public UpdateLoadInfo() {

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			String result = null;
			HttpResponse response = null;
			BufferedReader reader = null;
			try {
				
				ArrayList<NameValuePair> vs = new ArrayList<NameValuePair>();
				EditText edit1 = (EditText)findViewById(R.id.editText1);
				vs.add(new BasicNameValuePair("newPassword",userInfo.MD5(edit1.getText().toString())));

				response = LinkHelp.post(URLS.getModifyPW(), vs, false);

				if (response != null) {
					StatusLine line = response.getStatusLine();
					if (line.getStatusCode() == 200) {
						reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
						StringBuilder sb = new StringBuilder();
						for (String s = reader.readLine(); s != null; s = reader.readLine()) {
							sb.append(s);
						}
						reader.close();
						result = sb.toString();
						
						System.out.println(result);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(result);
                if (jsonObject.isNull("code")) {
                    String status = jsonObject.getString("status");
                    UserInfo info = UserInfo.getUserInfor();
                    if (status.equals("success")) {
                        Toast.makeText(ModifyPwActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        EditText edit1 = (EditText)findViewById(R.id.editText1);
                        userInfo.setPassword(edit1.getText().toString());
                        finish();
                    } else {
                    	String msg = jsonObject.getString("msg");
                    	//Toast.makeText(ModifyPwActivity.this, msg, Toast.LENGTH_SHORT).show();
                    	ToastHelp.doToastInTop(ModifyPwActivity.this, msg);
                    }
                    
                }}  
            catch (JSONException e) {
                e.printStackTrace();
            }
		}

	}
	
	public boolean isCanUpload()  {
		EditText edit = (EditText)findViewById(R.id.editText);
		if (!userInfo.MD5(edit.getText().toString()).equals(userInfo.getPassword())) 
		{
			//Toast.makeText(ModifyPwActivity.this, "密码验证错误", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(ModifyPwActivity.this, "密码验证错误");
			return false;
		}
		
		edit = (EditText)findViewById(R.id.editText1);
		EditText edit1 = (EditText)findViewById(R.id.editText2);
		if (edit.getText().toString().equals("")) {
			//Toast.makeText(ModifyPwActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(ModifyPwActivity.this, "密码不能为空");
			return false;
		}
		
		if (!edit.getText().toString().equals(edit1.getText().toString())) {
			//Toast.makeText(ModifyPwActivity.this, "俩次密码不一致", Toast.LENGTH_SHORT).show();
			ToastHelp.doToastInTop(ModifyPwActivity.this, "俩次密码不一致");
			return false;
		}

		return true;
	}

}
